package com.dayouzi.crawler_monitor.service.impl;

import com.dayouzi.crawler_monitor.mapper.DashboardMapper;
import com.dayouzi.crawler_monitor.pojo.vo.LineChartData;
import com.dayouzi.crawler_monitor.pojo.vo.PanelGroupVO;
import com.dayouzi.crawler_monitor.pojo.vo.RankDataListVO;
import com.dayouzi.crawler_monitor.pojo.vo.RankDataVO;
import com.dayouzi.crawler_monitor.service.DashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DashboardServiceImpl implements DashboardService {

    @Autowired
    private DashboardMapper dashboardMapper;

    @Override
    public PanelGroupVO getPanelGroupNum() {
        PanelGroupVO panelGroupVO = dashboardMapper.getPanelGroupNum();
        panelGroupVO.setOtherNum(99);
        return panelGroupVO;
    }

    @Override
    public LineChartData initLineChartData() {
        LineChartData lineChartData = new LineChartData();
        // 从数据库获取数据
        List<Integer> spiderNum = dashboardMapper.getSpiderNum();
        List<Integer> countNum = dashboardMapper.getCountNum();
        List<Integer> errorNum = dashboardMapper.getErrorNum();

        // 赋值
        lineChartData.setSpiderNum(spiderNum);
        lineChartData.setCountNum(countNum);
        lineChartData.setErrorNum(errorNum);
        lineChartData.setOtherNum(new ArrayList<Integer>() {{ // other暂时想不到需要展示啥，故全展示99
            for (int i = 0; i < 7; i++) {
                add(99);
            }
        }});
        return lineChartData;
    }

    @Override
    public RankDataListVO initRankData(String type, String date) {
        RankDataListVO result = new RankDataListVO();
        List<RankDataVO> rankListData = new ArrayList<>();
        List<RankDataVO> rankErrorData = new ArrayList<>();
        if (type.equals("count")) {
            rankListData = dashboardMapper.getRankListData(date);
        } else {
            rankErrorData = dashboardMapper.getRankErrorData(date);
        }
        for (int i = 0; i < rankListData.size(); i++) {
            rankListData.get(i).setRankNo(i + 1);
        }
        for (int i = 0; i < rankErrorData.size(); i++) {
            rankErrorData.get(i).setRankNo(i + 1);
        }
        result.setRankListData(rankListData);
        result.setRankErrorData(rankErrorData);
        return result;
    }
}
